﻿
using IdentityModel;
using IdentityModel.Client;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Models.Common;
using Models.Identity;
using Models.JsonResultModel;
using Models.WebClient;
using SqlSugar;
using System;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Net.Http.Headers;
using System.Security.Principal;
using UtilsORM;
using UtilsORM.MySqlData;
using UtilsORM.MySqlData;
using ServiceWeb.APIRequest;
using ServiceWeb.DBGateway;
using UtilsBase;
using UtilsORM.MySqlData;
using GatewayServer.Controllers.API.Common;
namespace GatewayServer.Controllers.API.Mobile
{
    [Route("api/mobile/[controller]/[action]")]
    [ApiController]
    //  [BasicAuthentication]
    public class GatewayController : ApiCommonControllerBase
    {

        /// <summary>
        /// 设备控制
        /// </summary>
        /// <param name="dguid"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        /// 
       
        public RespResultData getGatewayDetail(string dguid)
        {
            return RespResult.Sucess(new DBIotDeviceGateway().getGatewayDetail(dguid));
        }


       
        public RespResultData saveSendRestart(string dguid)
        {


            iot_device_gateway gwmodel = new EFYihongIotContext().iot_device_gateway.Where(c => c.gw_d_guid == dguid).FirstOrDefault();
            new ServiceDeviceControl().sendbashCommand(gwmodel.gw_code, "reboot", "app端");
            return RespResult.Sucess();
        }
       
        public RespResultData saveWifi(string dguid, string name, string pwd)
        {
            name = name.Trim();
            pwd = pwd.Trim();
            if (string.IsNullOrWhiteSpace(name))
            {
                return RespResult.Faild("wifi名称不能为空");
            }
            if (string.IsNullOrWhiteSpace(pwd))
            {
                return RespResult.Faild("wifi密码不能为空");
            }
            string sql = "select * from iot_device_gateway where  gw_d_guid='" + dguid + "' ";
            DataTable dt = new MySqlYihongIot().Db.Ado.GetDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                new EFYihongIotContext().iot_device_gateway.Where(c => c.gw_d_guid == dguid).ExecuteUpdate(c =>
                c.SetProperty(p => p.gw_wifi_name, name)
                .SetProperty(p => p.gw_wifi_pwd, pwd));

                //写入数据库
                //iot_device_gateway_wifi wifi = new iot_device_gateway_wifi();
                //wifi.wf_d_guid = dt.Rows[0]["gw_d_guid"].ToString();
                //wifi.wf_wifi_name = name;
                //wifi.wf_wifi_pwd = pwd;
                string result = new DBIotDeviceGatewayWifi().Add(name, pwd, dt.Rows[0]["gw_code"].ToString());
                return RespResult.Sucess("");
            }
            return RespResult.Faild();
        }

    }
}
